package com.example.xy_assistant;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MusicActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MusicActivity";

    private MediaPlayer mediaPlayer;
    private TextView tv_time;
    private TextView tv_total_time;
    private ImageButton btn_play;
    private ImageButton btn_pause;
    private ImageButton btn_stop;
    private SeekBar sb_progress;
    private boolean isOnTouch = false;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            tv_time.setText(getTime(msg.what));
            if (!isOnTouch&&mediaPlayer!=null) {
                sb_progress.setProgress(msg.what*100 / mediaPlayer.getDuration() );
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        initView();
        mediaPlayer = MediaPlayer.create(this, R.raw.music);
        initMedia();
    }

    private void initMedia() {
        tv_time.setText(getTime(mediaPlayer.getCurrentPosition()));
        tv_total_time.setText(getTime(mediaPlayer.getDuration()));
        new Thread(() -> {
            while (mediaPlayer != null) {
                handler.sendEmptyMessage(mediaPlayer.getCurrentPosition());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        sb_progress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isOnTouch = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mediaPlayer.seekTo(mediaPlayer.getDuration()*seekBar.getProgress()/100);
                isOnTouch = false;
            }
        });
    }

    private String getTime(int time) {
        if (time == 0) return "00:00";
        else {
            int m = time / 1000 / 60;
            int s = time / 1000 - m * 60;
            String mStr = m < 10 ? "0" + m : String.valueOf(m);
            String sStr = s < 10 ? "0" + s : String.valueOf(s);
            return mStr + ":" + sStr;
        }
    }

    private void initView() {
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_total_time = (TextView) findViewById(R.id.tv_total_time);
        btn_play = (ImageButton) findViewById(R.id.btn_play);
        btn_play.setOnClickListener(this);
        btn_pause = (ImageButton) findViewById(R.id.btn_pause);
        btn_pause.setOnClickListener(this);
        btn_stop = (ImageButton) findViewById(R.id.btn_stop);
        btn_stop.setOnClickListener(this);
        sb_progress = (SeekBar) findViewById(R.id.sb_progress);
        sb_progress.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_play:
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start();
                }
                break;
            case R.id.btn_pause:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
                break;
            case R.id.btn_stop:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    sb_progress.setProgress(0);
                    mediaPlayer = MediaPlayer.create(this,R.raw.music);
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        mediaPlayer.release();
        mediaPlayer = null;
        super.onDestroy();
    }
}